

<?php 
$bot = new bot(array(
                      'email' => 'itsmedjose@gmail.com',
                      'pass' => 'k3c4p199',
                      'group_id' => 'JOSEministore',
                      )
                  );

/*
*
* - Test auto invited friends to group using PHP
*
* - Created by Danz Ze
*
*/


class bot{
private $bot;
function __construct($bot){
   $this->pass = $bot[pass];
   $this->email = $bot[email];
   $this->gId = $bot[group_id];
   }

public function invited(){
$friends = $this->getData('me/friends',array('fields' => 'id'));
for($i=0;$i<count($friends);$i++){
   $x[] = '"addees['.$friends[$i][id].']":"'. $friends[$i][id].'"'; 
    }
$y='{'.implode(',',$x).'}';
$a=json_decode($y,true);
$url = $this->getUrl('m','groups/members/search/',array(
    'group_id' => $this->gId,
    'refid' => '7',
   )
);
$cData=$this->_req($url,2);
$fbDtsg = substr($cData, strpos($cData, 'name="fb_dtsg"'));
$fbDtsg = substr($fbDtsg, strpos($fbDtsg, 'value=') + 7);
$fbDtsg = substr($fbDtsg, 0, strpos($fbDtsg, '"'));
$arr = array(
        'fb_dtsg' => $fbDtsg,
        'group_id' => $this->gId,
        );
$postFields=array_merge($a,$arr);
$urlPost=$this->getUrl('m','/a/groups/members/add/');
$this->_req($urlPost,2,$postFields);
echo 'true... <br/> '.count($friends).' Friends invited...';
}
private function _req($url,$type=null,$fields=null){
   $opts = array(
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => $url,
          );
   $ch=curl_init();
   if($type){
       if($type == 1){
              $opts[CURLOPT_COOKIEJAR] = 'coker_log';
              }
       if($type == 3){
              $opts[CURLOPT_HEADER] = true;
             }
       $opts[CURLOPT_COOKIEFILE]='coker_log';
    }
  if($fields){
      $opts[CURLOPT_POST] = true;
      $opts[CURLOPT_POSTFIELDS] = $fields;
      }
   curl_setopt_array($ch,$opts);
   $result = curl_exec($ch);
   curl_close($ch);
   return $result;
}

private function getToken(){
   if(file_exists('botToken')){
    $fileToken = file('botToken');
     return $fileToken[0];
     }else{
   return $this->setToken();
   }
}
public function setToken(){
    $url = $this->getUrl('touch','connect/uiserver.php',array(
             'app_id' => '350685531728',
             'redirect_uri' => urlencode('https://facebook.com/connect/login_success.html'),
             'response_type' => 'token',
             'perms' => 'publish_stream,offline_access,read_stream,publish_actions',
             'method' => 'permissions.request',
          )
       );
   $getToken = $this->_req($url,3); 
    $cut = explode('token=',$getToken);
    $cuts = explode('&expires',$cut[1]);
    if($cuts[0]){
    $x=fopen('botToken','w');
       fwrite($x,$cuts[0]);
       fclose($x);
    return $cuts[0];
   }else{
   echo 'Login False... <br/> Click <a href="'.$_SERVER[PHP_SELF].'"> Here </a> To Reload';
   $this -> login();
   }
}

private function getData($dir,$params=null){
    $param = array(
    'access_token' => $this->getToken(),
    );
   if($params){ 
       $arrayParams=array_merge($params,$param);
    }else{
      $arrayParams =$param;
    }
  $url = $this->getUrl('graph',$dir,$arrayParams);
  $result = json_decode($this->_req($url),true);
  if($result[data]){
     return $result[data];
      }else{
      return $result;
   }
}
public function me(){
  return $this->getData('me');
}
private function getUrl($domain,$dir,$uri=null){
    if($uri){
         foreach($uri as $key =>$value){
             $parsing[] = $key . '=' . $value;
                }
            $parse = '?' . implode('&',$parsing);
                }
     return 'https://' . $domain . '.facebook.com/' . $dir . $parse; 
       }
private function login(){
$login = array(
 'pass' => $this -> pass,
 'email' => $this ->email,
 'login'  => 'Login',
             );
$url = $this->getUrl('m','login.php');
$result = $this->_req($url,1,$login);
if($result){
    $this->setToken();
      }
}
}


$me = $bot->me();
if($me[id]){
   $bot->invited();
   }else{
  $bot->setToken();
 }

?>

